package com.zjm.principle.crp;

/**
 * 合成复用原则
 */
public class CompositeReuse02 {
    public static void main(String[] args) {
        PaymentStrategy alipay = new Alipay1();
        PaymentContext context = new PaymentContext(alipay);
        context.executePayment(); // 输出：使用支付宝支付
    }
}

// 支付策略接口
interface PaymentStrategy {
    void pay();
}

// 具体支付方式
class Alipay1 implements PaymentStrategy {
    @Override
    public void pay() {
        System.out.println("使用支付宝支付");
    }
}

class WechatPay1 implements PaymentStrategy {
    @Override
    public void pay() {
        System.out.println("使用微信支付");
    }
}

class UnionPay1 implements PaymentStrategy {
    @Override
    public void pay() {
        System.out.println("使用银联支付");
    }
}

// 上下文类，通过组合方式使用支付策略
class PaymentContext {
    private PaymentStrategy strategy;

    public PaymentContext(PaymentStrategy strategy) {
        this.strategy = strategy;
    }

    public void executePayment() {
        strategy.pay();
    }
}
